﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;

namespace Site.Secure
{
    public partial class MapView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Populate_Markers("SELECT * FROM Sites");
        }

        private void Populate_Markers(string sSelect)
        {
            DataSourceSelectArguments Select = new DataSourceSelectArguments();
            SqlDataSource1.SelectCommand = sSelect;
            DataView View = (DataView)SqlDataSource1.Select(Select);

            string Barcode;
            string SiteID;
            string GPS;
            string SupersCount;
            string Brood;
            string FullFrames;
            string Feed;
            string Capped;
            string Feeding;
            string Attention;
            string Harvest;
            string Problem;
            string Queen;
            string PID;

            DateTime LastUpdatedDate;
            string LastUpdatedTime;

            double Long;
            double Lat;

            string sLong;
            string sLat;

            //Artem.Google.UI.GooglePolygon poly = new Artem.Google.UI.GooglePolygon();
            

            int x = 1;
            gmHives.Markers.Clear();
            ListBox1.Items.Clear();
            ListBox lb = new ListBox();
            lb.Visible = true;
            //gmHives.Overlays.Add(new Artem.Google.UI.Overlay());
            foreach (DataRowView DR in View)
            {

                Artem.Google.UI.Marker m = new Artem.Google.UI.Marker();
                Long = Double.Parse(DR["posLong"].ToString());
                Lat = Double.Parse(DR["posLat"].ToString());
                m.Title = DR["SiteID"].ToString();

                m.Position.Latitude = Lat;
                m.Position.Longitude = Long;

                m.Info = "<h3>" + DR["SiteID"] + "</h3>";
                m.Info += "<a>Current Weather Conditions: " + DR["weatherType"] + "</a><br/>";
                m.Info += "<a>Temprature: " + DR["weatherTemp"] + "°C</a><br/>";
                m.Info += "<a>Hive State: " + DR["State"] + "</a><br/>";
                m.Info += "<a>Last Updated On: " + DateTime.Parse(DR["lastUpdated"].ToString())  + "</a><br/>";

                gmHives.Markers.Add(m);
                lblMarkers.Text = x.ToString() + " ";
                x++;
            }
               
            //gmHives.Overlays.Add(poly);
            //System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('There are " + ListBox1.Items.Count + " Markers in listview')</SCRIPT>");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Populate_Markers("SELECT DISTINCT Barcode FROM DataTable WHERE " + DropDownList1.Text + "= '" + TextBox1.Text + "'");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Populate_Markers("SELECT DISTINCT BarcodeFROM DataTable WHERE " + DropDownList2.Text + " BETWEEN '" + edtB1.Text + "' AND '" + edtB2.Text + "'");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Populate_Markers("SELECT DISTINCT Barcode FROM DataTable WHERE " + TextBox3.Text);
        }
    }
}